package plp.orientadaObjetos2.ambiente.bootstrap;

import java.io.FileInputStream;
import java.io.InputStream;

import plp.orientadaObjetos1.memoria.colecao.ListaValor;
import plp.orientadaObjetos2.Programa;
import plp.orientadaObjetos2.memoria.AmbienteCompilacaoOO2;
import plp.orientadaObjetos2.memoria.AmbienteExecucaoOO2;
import plp.orientadaObjetos2.memoria.ContextoCompilacaoOO2;
import plp.orientadaObjetos2.memoria.ContextoExecucaoOO2;

public class Bootstrapper {
	//public static final String BOOTSTRAP_DEFINITIONS = "plp/plp.orientadaObjetos2/ambiente/Runtime.oo2";
    public static final String BOOTSTRAP_DEFINITIONS = "exemplos/runtime/Runtime.oo2";

    private AmbienteCompilacaoOO2 ambienteCompilacao;
    private AmbienteExecucaoOO2 ambienteExecucao;

    private boolean bootstrapped;

	private Parser parser;

    public Bootstrapper(Parser parser){
    	this.parser = parser;
    }

    public AmbienteCompilacaoOO2 getAmbienteCompilacao() {
        return this.ambienteCompilacao;
    }
    public AmbienteExecucaoOO2 getAmbienteExecucao() {
        return this.ambienteExecucao;
    }

    public void bootstrap() {
        if(!this.bootstrapped){
            try{
            	//InputStream runtimeClassesStream = getClass().getResourceAsStream(BOOTSTRAP_DEFINITIONS);
                InputStream runtimeClassesStream = new FileInputStream(BOOTSTRAP_DEFINITIONS);
                Programa bootstrap = this.parser.processaEntrada(runtimeClassesStream);
                bootstrap.setNomeArquivo("Runtime");
                this.ambienteCompilacao = new ContextoCompilacaoOO2(new ListaValor());
                bootstrap.checaTipo(this.ambienteCompilacao);
                this.ambienteExecucao = new ContextoExecucaoOO2();
                bootstrap.executar(this.ambienteExecucao);
                this.bootstrapped = true;
            } catch(Exception e){
                throw new BootstrappingException(e);
            }
        }
    }
}